package ntd.util.frame.task.handle.fm;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Entity;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.StaticLog;
import com.tool.ToolRedisson;
import ntd.task.Task;
import ntd.task.bo.TaskInfo;
import ntd.util.config.common.bo.RequestInfo;
import ntd.util.frame.task.handle.fm.util.ToolFmData;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class FmNewDataCache implements Task {

    @Override
    public TaskInfo info() {
        return new TaskInfo("富铭-实时数据缓存任务", "task-fm-new-data-cache", new FmNewDataCache());
    }

    @Override
    public void execute(Object o) {
        RequestInfo convert = Convert.convert(RequestInfo.class, ObjectUtil.defaultIfNull(o, new RequestInfo()));
        // 查询实时数据拼装
        List<Entity> newset = ToolFmData.newset(convert.getDataCode());
        // 存入缓存
        List<JSONObject> jsonObjects = new ArrayList<>();
        for (Entity entity : new ArrayList<>(newset)) {
            JSONObject jsonObject = JSONUtil.parseObj(entity);
            JSONObject newJson = new JSONObject();
            Set<String> keySet = jsonObject.keySet();
            for (String key : keySet) {
                Object val = jsonObject.get(key);
                newJson.putOnce(StrUtil.toCamelCase(key), val);
            }
            JSONArray items = jsonObject.getJSONArray("items");
            JSONArray newItems = new JSONArray();
            for (int i = 0; i < items.size(); i++) {
                JSONObject newItem = new JSONObject();
                for (Map.Entry<String, Object> stringObjectEntry : items.getJSONObject(i).entrySet()) {
                    String key = stringObjectEntry.getKey();
                    newItem.putOnce(StrUtil.toCamelCase(key), stringObjectEntry.getValue());
                }
                newItems.put(newItem);
            }
            newJson.putOpt("items", newItems);
            jsonObjects.add(newJson);
        }
        for (JSONObject jsonObject : jsonObjects) {
            String key = "data:newest:" + jsonObject.getStr("cn") + ":" + jsonObject.getStr("mn");
            StaticLog.info("实时数据缓存[{}]", key);
            ToolRedisson.me(0).set(key, jsonObject.toString());
        }
    }

    public static void main(String[] args) {
//        new FmNewDataCache().execute(new RequestInfo());
//        new FmNewDataCache().execute(new RequestInfo());
//        ToolMysql toolMysql = new ToolMysql(FactoryData.dataSource());
//        Entity commonTask = toolMysql.findById("common_task", "6286fa6373907fe657d326ed25c7ecbb");
//        TaskInfo taskInfo = TaskManager.get(commonTask.getStr("task_code"));
        new FmNewDataCache().execute(null);
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.set("test", "123456");
//        ToolRedisson.me(0).set("test", "测试数据531531");
    }

}
